package org.keyboardman.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by longky on 2017/3/28.
 */
@Controller
public class AccountController {
    @RequestMapping("/dologin")
    public String doLogin(HttpServletRequest request) {

        String username = request.getParameter("loginname");
        String pwd = request.getParameter("password");

        String captcha = request.getParameter("captcha");
        String remberme = request.getParameter("remberme");
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, pwd, captcha);
            if (StringUtils.isNotEmpty(remberme))
                token.setRememberMe(true);
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.login(token);
        } catch (UnknownAccountException e) {
            //账号不存在
            return "login.html?message='UnknownAccount";
        } catch (org.apache.shiro.authc.IncorrectCredentialsException e) { //密码错误
            return "login.html?message='passworderror";
        } catch (LockedAccountException e) {
            //账号被锁定
            return "index.htmll?message=accountlocked";
        } catch (Exception e) {
            //其他问题
            return "index.htm?message=others";
        }
        return "index.html";
    }
}


